package com.hxd.otc.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.hxd.otc.R;
import com.hxd.otc.base.OnItemClickListener;
import com.hxd.otc.module.convenience.adapter.PaymentMethodAdapter;
import com.hxd.otc.module.convenience.bean.PaymentMethodBean;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 包  名 :  com.hxd.otc.widget
 * 类  名 :  SuccessDialog
 * 作  者 :  卫广玉
 * 时  间 :  2018/6/12 17:10
 * 描  述 :  ${TODO}
 */
public class PayMethodDialog extends Dialog {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.iv_close)
    ImageView iv_close;

    private Unbinder mBind;
    PaymentMethodAdapter adapter;
    ArrayList<PaymentMethodBean> paymentMethodBeans;

    public PayMethodDialog(@NonNull Context context, ArrayList<PaymentMethodBean> paymentMethodBeans) {
        this(context, R.style.BlockDialog);
        this.paymentMethodBeans = paymentMethodBeans;
    }

    public PayMethodDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_payment_method);
        mBind = ButterKnife.bind(this);
        initWindows();
    }

    public void setPaymentMethodBeans(ArrayList<PaymentMethodBean> paymentMethodBeans) {
        this.paymentMethodBeans = paymentMethodBeans;
        adapter.addAll(true, paymentMethodBeans);
    }

    private void initWindows() {

        setCanceledOnTouchOutside(true);
        Window window = getWindow();
        if (window != null) {
            DisplayMetrics dm = new DisplayMetrics();
            window.getWindowManager().getDefaultDisplay().getMetrics(dm);
            WindowManager.LayoutParams windowAttributes = window.getAttributes();
            windowAttributes.width = WindowManager.LayoutParams.MATCH_PARENT;
            windowAttributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
            windowAttributes.gravity = Gravity.BOTTOM;
            window.setAttributes(windowAttributes);
        }
        initView();

    }

    private void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        adapter = new PaymentMethodAdapter(getContext());
        recyclerView.setAdapter(adapter);


        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (payMethodCallBack != null) {
                    payMethodCallBack.onClick(adapter.getItem(position));
                }
            }
        });
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }


    PayMethodCallBack payMethodCallBack;

    public void setPayMethodCallBack(PayMethodCallBack payMethodCallBack) {
        this.payMethodCallBack = payMethodCallBack;
    }

    public interface PayMethodCallBack {
        void onClick(PaymentMethodBean bean);

    }
}
